package 锁;

import java.util.Scanner;

//线程不安全问题：内存可见性
public class Demo_5 {
//这里即使修改flg的值也不会影响thread1线程的执行---flg变量的读写存在可见性问题
 volatile static int flg = 0;

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            while (flg == 0) {

            }
            System.out.println("执行完毕");
        });

        Thread thread2 = new Thread(() -> {
            System.out.println("修改flg的值");
            Scanner scanner = new Scanner(System.in);
            flg = scanner.nextInt();
            scanner.close();

        });

        thread2.start();
        thread1.start();
    }
}
